//
//  BasePresentationController.m
//  wozhua
//
//  Created by 王洋 on 16/3/22.
//  Copyright © 2016年 王洋. All rights reserved.
//

#import "BasePresentationController.h"
#import "MZFormSheetController.h"
#import "CrossVersionProtocol.h"

static NSMutableArray *_instanceOfSharedQueue = nil;

@implementation UIViewController (BasePresentationControllerPrivate)

- (BasePresentationController *)basePresentationController {
    return objc_getAssociatedObject(self, @selector(basePresentationController));
}

- (void)setBasePresentationController:(BasePresentationController * _Nullable)basePresentationController {
    objc_setAssociatedObject(self, @selector(basePresentationController), basePresentationController, OBJC_ASSOCIATION_ASSIGN);
}

@end

@interface BasePresentationController () <CrossVersionProtocol>

@property (nonatomic, strong) MZFormSheetController *oldController;
@property (nonatomic, strong) MZFormSheetPresentationViewController *presentationViewController;
@property (nonatomic, weak) UIViewController *presentingViewController;

@end

@implementation BasePresentationController

+ (void)load {
    @autoreleasepool {
        _instanceOfSharedQueue = [[NSMutableArray alloc] init];
    }
}

+ (BOOL)isAnimating {
    if ([self useNewAPI]) {
        return NO;
    } else {
        return [MZFormSheetController isAnimating];
    }
}

+ (NSMutableArray *)sharedQueue {
    return _instanceOfSharedQueue;
}

+ (NSArray * __nonnull)presentationControllersStack {
    return [[self sharedQueue] copy];
}

- (nonnull instancetype)initWithContentViewController:(UIViewController * __nonnull)viewController {
    self = [super init];
    
    if (self) {
        if ([[self class] useNewAPI]) {
            self.presentationViewController = [[MZFormSheetPresentationViewController alloc] initWithContentViewController:viewController];
        } else {
            self.oldController = [[MZFormSheetController alloc] initWithViewController:viewController];
        }
        viewController.basePresentationController = self;
    }
    
    return self;
}

- (void)presentFromViewController:(UIViewController * __nonnull )presentingViewController animated: (BOOL)flag completion:(void (^ __nullable)(void))completion {
    if (![[[self class] sharedQueue] containsObject:self]) {
        [[[self class] sharedQueue] addObject:self];
    }
    
    if ([[self class] useNewAPI]) {
        self.presentingViewController = presentingViewController;
        [presentingViewController presentViewController:self.presentationViewController animated:flag completion:completion];
    } else {
        WEAKSELF
        [presentingViewController mz_presentFormSheetController:self.oldController animated:flag completionHandler:^(MZFormSheetController * _Nonnull formSheetController) {
            [MZFormSheetController sharedBackgroundWindow].supportedInterfaceOrientations = [weakSelf.oldController supportedInterfaceOrientations];
            if (completion) {
                completion();
            }
        }];
    }
}

- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion {
    [[[self class] sharedQueue] removeObject:self];
    
    WEAKSELF
    if ([[self class] useNewAPI]) {
        [self.presentingViewController dismissViewControllerAnimated:flag completion:^{
            weakSelf.presentationViewController.contentViewController.basePresentationController = nil;
            if (completion) {
                completion();
            }
        }];
    } else {
        [self.oldController dismissAnimated:flag completionHandler:^(UIViewController * _Nonnull presentedFSViewController) {
            weakSelf.oldController.presentedFSViewController.basePresentationController = nil;
            if (completion) {
                completion();
            }
        }];
    }
}

#pragma mark - CrossVersionProtocol

+ (BOOL)useNewAPI {
    return kIsAfterIOS8;
}

#pragma mark - getters and setters

- (CGFloat)contentViewCornerRadius {
    if ([[self class] useNewAPI]) {
        return self.presentationViewController.contentViewCornerRadius;
    } else {
        return self.oldController.cornerRadius;
    }
}

- (void)setContentViewCornerRadius:(CGFloat)contentViewCornerRadius {
    if ([[self class] useNewAPI]) {
        self.presentationViewController.contentViewCornerRadius = contentViewCornerRadius;
    } else {
        self.oldController.cornerRadius = contentViewCornerRadius;
    }
}

- (CGFloat)shadowRadius {
    if ([[self class] useNewAPI]) {
        return self.presentationViewController.shadowRadius;
    } else {
        return self.oldController.shadowRadius;
    }
}

- (void)setShadowRadius:(CGFloat)shadowRadius {
    if ([[self class] useNewAPI]) {
        self.presentationViewController.shadowRadius = shadowRadius;
    } else {
        self.oldController.shadowRadius = shadowRadius;
    }
}

- (MZFormSheetPresentationTransitionStyle)contentViewControllerTransitionStyle {
    if ([[self class] useNewAPI]) {
        return self.presentationViewController.contentViewControllerTransitionStyle;
    } else {
        NSNumber *style = [NSNumber numberWithInt:self.oldController.transitionStyle];
        return [style intValue];
    }
}

- (void)setContentViewControllerTransitionStyle:(MZFormSheetPresentationTransitionStyle)contentViewControllerTransitionStyle {
    if ([[self class] useNewAPI]) {
        self.presentationViewController.contentViewControllerTransitionStyle = contentViewControllerTransitionStyle;
    } else {
        MZFormSheetTransitionStyle style = MZFormSheetTransitionStyleNone;
        
        switch (contentViewControllerTransitionStyle) {
            case MZFormSheetPresentationTransitionStyleSlideFromTop:
                style = MZFormSheetTransitionStyleSlideFromTop;
                break;
                
            case MZFormSheetPresentationTransitionStyleSlideFromBottom:
                style = MZFormSheetTransitionStyleSlideFromBottom;
                break;
                
            case MZFormSheetPresentationTransitionStyleSlideFromLeft:
                style = MZFormSheetTransitionStyleSlideFromLeft;
                break;
                
            case MZFormSheetPresentationTransitionStyleSlideFromRight:
                style = MZFormSheetTransitionStyleSlideFromRight;
                break;
               
            case MZFormSheetPresentationTransitionStyleDropDown:
                style = MZFormSheetTransitionStyleDropDown;
                break;
            
            case MZFormSheetPresentationTransitionStyleFade:
                style = MZFormSheetTransitionStyleFade;
                break;
            
            case MZFormSheetPresentationTransitionStyleBounce:
                style = MZFormSheetTransitionStyleBounce;
                break;
                
            default:
                break;
        }
        self.oldController.transitionStyle = style;
    }
}

- (CGSize)contentViewSize {
    if ([[self class] useNewAPI]) {
        return self.presentationViewController.presentationController.contentViewSize;
    } else {
        return self.oldController.presentedFormSheetSize;
    }
}

- (void)setContentViewSize:(CGSize)contentViewSize {
    if ([[self class] useNewAPI]) {
        self.presentationViewController.presentationController.contentViewSize = contentViewSize;
    } else {
        self.oldController.presentedFormSheetSize = contentViewSize;
    }
}

- (CGFloat)landscapeTopInset {
    if ([[self class] useNewAPI]) {
        return self.presentationViewController.presentationController.landscapeTopInset;
    } else {
        return self.oldController.landscapeTopInset;
    }
}

- (void)setLandscapeTopInset:(CGFloat)landscapeTopInset {
    if ([[self class] useNewAPI]) {
        self.presentationViewController.presentationController.landscapeTopInset = landscapeTopInset;
    } else {
        self.oldController.landscapeTopInset = landscapeTopInset;
    }
}

- (CGFloat)portraitTopInset {
    if ([[self class] useNewAPI]) {
        return self.presentationViewController.presentationController.portraitTopInset;
    } else {
        return self.oldController.portraitTopInset;
    }
}

- (void)setPortraitTopInset:(CGFloat)portraitTopInset {
    if ([[self class] useNewAPI]) {
        self.presentationViewController.presentationController.portraitTopInset = portraitTopInset;
    } else {
        self.oldController.portraitTopInset = portraitTopInset;
    }
}

- (BOOL)shouldDismissOnBackgroundViewTap {
    if ([[self class] useNewAPI]) {
        return self.presentationViewController.presentationController.shouldDismissOnBackgroundViewTap;
    } else {
        return self.oldController.shouldDismissOnBackgroundViewTap;
    }
}

- (void)setShouldDismissOnBackgroundViewTap:(BOOL)shouldDismissOnBackgroundViewTap {
    if ([[self class] useNewAPI]) {
        self.presentationViewController.presentationController.shouldDismissOnBackgroundViewTap = shouldDismissOnBackgroundViewTap;
    } else {
        self.oldController.shouldDismissOnBackgroundViewTap = shouldDismissOnBackgroundViewTap;
    }
}

- (BOOL)shouldCenterVertically {
    if ([[self class] useNewAPI]) {
        return self.presentationViewController.presentationController.shouldCenterVertically;
    } else {
        return self.oldController.shouldCenterVertically;
    }
}

- (void)setShouldCenterVertically:(BOOL)shouldCenterVertically {
    if ([[self class] useNewAPI]) {
        self.presentationViewController.presentationController.shouldCenterVertically = shouldCenterVertically;
    } else {
        self.oldController.shouldCenterVertically = shouldCenterVertically;
    }
}

- (UIView *)dimmingView {
    if ([[self class] useNewAPI]) {
        return self.presentationViewController.presentationController.dimmingView;
    } else {
        return [MZFormSheetController sharedBackgroundWindow];
    }
}

@end

@implementation UIViewController (BasePresentationController)

@dynamic basePresentationController;

- (BOOL)hasBasePresentedController {
    if ([BasePresentationController useNewAPI]) {
        MZFormSheetPresentationViewController *dvc = [self mz_formSheetPresentedPresentationController];
        return dvc != nil;
    } else {
        return [MZFormSheetController formSheetControllersStack].count > 0;
    }
}

- (void)setHasBasePresentedController:(BOOL)hasBasePresentedController {
    
}

- (void)dismissPresentationControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
    if (self.basePresentationController) {
        //被弹出控制器调用
        [self.basePresentationController dismissViewControllerAnimated:flag completion:completion];
    } else {
        //其他控制器调用
        BasePresentationController *targetController = [[BasePresentationController presentationControllersStack] firstObject];
        if (targetController) {
            [targetController dismissViewControllerAnimated:flag completion:completion];
        }
    }
}

@end

